using System;
using System.Globalization;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Win32;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;
using System.Collections.Specialized;

using Google.GData.Photos;
using Google.GData.Extensions.MediaRss;
using McePicasa.mceFlickr;

[assembly: CLSCompliant(true)]
namespace McePicasa
{
    /// <summary>
    /// This is the global coordinating object for the McePicasa application.
    /// It handles the general page navigation.
    /// </summary>
    public class Application : ModelItem
    {
        /// <summary>
        /// Empy constructor to enable markup creation for when we want
        /// to run in McmlPad standalone.
        /// </summary>
        public Application()
            : this(null, null)
        {
        }

        /// <summary>
        /// Construct an Application object and associate it with a page
        /// session.
        /// </summary>
        public Application(HistoryOrientedPageSession session, AddInHost host)
        {
            // Store the page session and host
            this.session = session;
            this.host = host;

            TrackUsers();

            Global.Init();

            // initialize the field used by the static Application.Current property
            singleApplicationInstance = this;

            appDataPath = Environment.ExpandEnvironmentVariables("%systemdrive%") + @"\McePicasa";

            try
            {
                if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath);
            }
            catch (IOException e)
            {
                Global.MsgBox(e.Message, 20);
                GoUp();
            }

            //
            // Construct the application experiences
            //

            this.AppStatusText = "Loading Users...";
            AddExperience(new User());
            
            //Simple way to get the time-consuming routine to run in the next round of message loop
            //to enable the UI to update the status text

            StringCollection Users = Properties.Settings.Default.Users;
            StringCollection FlickrUsers = Properties.Settings.Default.UsersFlickr;
            StringCollection UsersEx = new StringCollection();
            StringCollection FlickrUsersEx = new StringCollection();
            UsersEx.Add("999wonders");
            if (Users != null)
            {
                foreach (string uid in Users)
                {
                    if (!UsersEx.Contains(uid))
                        UsersEx.Add(uid);
                }

            }
            if (FlickrUsers != null)
            {
                foreach (string uid in FlickrUsers)
                {
                    if (!FlickrUsersEx.Contains(uid))
                        FlickrUsersEx.Add(uid);
                }
            }

            Timer Cl;
            Cl = new Timer();
            Cl.Data.Add("users", UsersEx);
            Cl.Data.Add("flickrusers", FlickrUsersEx);
            Cl.Data.Add("index", 0);
            Cl.Data.Add("load", false);
            Cl.Interval = 100;
            Cl.Tick += new EventHandler(LoadUsers);
            Cl.Start();
        }

        private void LoadUsers(object sender, EventArgs args)
        {
            Timer t = (sender as Timer);
            StringCollection Users = (StringCollection)t.Data["users"];
            StringCollection FlickrUsers = (StringCollection)t.Data["flickrusers"];
            int i = (int)t.Data["index"];
            if (i < Users.Count)
            {

                string userid = Users[i];
                this.AppStatusText = String.Format(CultureInfo.CurrentCulture, "Loading user ({0}/{1}): [{2}] ...",
                    i + 1, Users.Count, userid == "999wonders" ? "Sample" : userid);
                
                if ((bool)t.Data["load"])
                {
                    t.Data["load"] = false;
                    PicasaFeed feed = PicasaUser.CheckUserId(userid);
                    if (feed != null)
                    {
                        
                        if (userid == "999wonders")
                        {
                            PicasaUser sampleUser = new PicasaUser(userid, feed);
                            AddExperience(new User(sampleUser));
                            PicasaAlbum ab = sampleUser.GetAlbum(0);
                            if (ab != null)
                                VersionManager.CheckForNewVersion(sampleUser.GetAlbum(0).Summary,
                                    sampleUser.GetAlbum(1).Summary);
                        }
                        else
                            AddExperience(new User(new PicasaUser(userid, feed)));
                    }
                    else
                    {
                        Global.MsgBox(String.Format(CultureInfo.CurrentCulture, "LOADING USER ({0}/{1}): [{2}] FAILED! CHECK YOUR INTERNET CONNECTION!!!",
                        i + 1, Users.Count, userid == "999wonders" ? "Sample" : userid), 5, false);
                    }
                    i++;
                    t.Data["index"] = i;


                    
                    foreach (string item in FlickrUsers)
                    {
                        AddExperience(new User(new FlickrUser(item)));
                    }



                }
                else
                    t.Data["load"] = true;
            }
            else
            {
                //System.Threading.Thread.Sleep(3000);
                this.AppStatusText = "";
                t.Stop();
                t.Dispose();
            }
        }

        /// <summary>
        /// Adds the given experience to the Application's Experiences list
        /// and create a menu category for it.
        /// </summary>
        private void AddExperience(ApplicationExperience experience)
        {
            // Application sets experience's Id to its index in the Experiences
            // array.  This makes it easy to map from id back to experience
            experience.MyId = experiences.Count;

            experiences.Add(experience);
            menu.Add(experience.CreateMenuCategory());
        }

        /// <summary>
        /// Given a unique experience id get the Experience object.
        /// </summary>
        public ApplicationExperience GetExperienceFromId(int id)
        {
            return experiences[id];
        }

        /// <summary>
        /// The one and only instance of Application running in this McePicasa process
        /// </summary>
        public static Application Current
        {
            get
            {
                return singleApplicationInstance;
            }
        }

        /// <summary>
        /// The main menu for the McePicasa application.
        /// Each item in this array is a MenuCategory
        /// </summary>
        public ArrayListDataSet MainMenu
        {
            get { return menu; }
        }

        /// <summary>
        /// The Media Center environment.
        /// </summary>
        public MediaCenterEnvironment MediaCenterEnvironment
        {
            get
            {
                if (host == null)
                    return null;

                return host.MediaCenterEnvironment;
            }
        }

        /// <summary>
        /// The current logged in user.
        /// </summary>
        public string UserName
        {
            get
            {
                return username;
            }

            private set
            {
                if (username != value)
                {
                    username = value;
                    FirePropertyChanged("UserName");
                }
            }
        }

        /// <summary>
        /// Navigate to the appropriate starting page for the app.
        /// </summary>
        public void GoToStartPage()
        {
            //
            // Skip past the log in page if a registry key is set.
            // This logic is purely for the purpose of demonstration.
            // Obviously if your application required a login in order to
            // proceed you wouldn't want to skip over the log in page.
            //

            // Get the value from the registry.
            // Accomodate for 64-bit support.
            string showLoginPageValue = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\McePicasa", @"ShowLoginPage", null) as string;
            if (showLoginPageValue == null)
                showLoginPageValue = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\McePicasa", @"ShowLoginPage", null) as string;

            // Map this from a string to a bool.
            //bool showLoginPage = (showLoginPageValue != "0");

            GoToMenu();
            //GoToLogIn();
            //GoToSearch();
            /*
            // Either go to the log in page or the main menu.
            if (showLoginPage)
                GoToLogIn();
            else
                GoToMenu();
             */
        }

        /// <summary>
        /// Navigate to the log in page.
        /// </summary>
        public void GoToLogOn()
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Application"] = this;

            if (session != null)
            {
                session.GoToPage("resx://McePicasa/McePicasa.Resources/LogInPage", properties);
            }
            else
            {
                Log.LogIt("GoToLogIn");
            }
        }


        /// <summary>
        /// Log in
        /// </summary>
        public void LogOn(string user, string password, bool rememberPassword)
        {
            //
            // The sample app does not validate log in credentials.
            // This is where you would implement your app's log in validation code.
            //

            Log.LogIt(String.Format(CultureInfo.CurrentCulture, "LogIn: {0} {1}{2}", user, password, rememberPassword ? " (remember)" : null));
            UserName = user;

            GoToMenu();
        }

        public void GoToLogOn0(User user, MenuCategory item)
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Application"] = this;
            properties["User"] = user;
            properties["ModelItem"] = item;

            if (session != null)
            {
                session.GoToPage("resx://McePicasa/McePicasa.Resources/LogInPage0", properties);
            }
            else
            {
                Log.LogIt("GoToLogIn0");
            }
        }

        public void GoToLogOn1(User user, MenuCategory item)
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Application"] = this;
            properties["User"] = user;
            properties["ModelItem"] = item;

            if (session != null)
            {
                session.GoToPage("resx://McePicasa/McePicasa.Resources/LogInPage1", properties);
            }
            else
            {
                Log.LogIt("GoToLogIn1");
            }
        }

        private void LoadUser(object sender, EventArgs args)
        {
            Timer t = (Timer)sender;
            t.Stop();

            //User user = (User)t.Data["user"];
            MenuCategory item = (MenuCategory)t.Data["item"];
            string user_name = (string)t.Data["username"];
            bool saveuser = (bool)t.Data["saveuser"];
            t.Dispose();

            PicasaFeed feed = PicasaUser.CheckUserId(user_name);
            if (feed != null)
            {
                if (saveuser && user_name.ToLower(CultureInfo.CurrentCulture) != "999wonders")
                {
                    System.Collections.Specialized.StringCollection Users = Properties.Settings.Default.Users;
                    System.Collections.Specialized.StringCollection newUsers =
                            new System.Collections.Specialized.StringCollection();

                    if (Users != null && Users.Count > 0)
                    {
                        newUsers.Add(user_name);
                        foreach (string User_Name in Users)
                        {
                            //Application.Current.MessageBox(User_Name, User_Name);
                            if (!newUsers.Contains(User_Name))
                            {
                                newUsers.Add(User_Name);
                            }
                            if (newUsers.Count >= 3) break;
                        }
                    }
                    else
                    {
                        newUsers.Add(user_name);
                    }
                    Properties.Settings.Default.Users = newUsers;
                    Properties.Settings.Default.Save();
                }

                PicasaUser picasaUser = new PicasaUser(user_name, feed);
                //user.PicasaUser = picasaUser;
                //user.Description = picasaUser.Id;
                User newUser = new User(picasaUser);
                GalleryPage page = newUser.CreateAlbumGalleryPage(item);
                Application.Current.GoToGallery(page);
                AddExperience(newUser);
            }
            else
            {
                Application.Current.MessageBox("UserID does not exist or does not have any public album!",
                    "http://picasaweb.google.com/" + user_name, 10, true);
            }
            this.AppStatusText = "";
        }


        private void LoadUserFlickr(object sender, EventArgs args)
        {
            Timer t = (Timer)sender;
            t.Stop();

            //User user = (User)t.Data["user"];
            MenuCategory item = (MenuCategory)t.Data["item"];
            string user_name = (string)t.Data["username"];
            bool saveuser = (bool)t.Data["saveuser"];
            t.Dispose();

            FlickrAPI fa = new FlickrAPI();
            bool resp = fa.CheckIfUserExists(user_name);

            if (resp)
            {
                if (saveuser && user_name.ToLower(CultureInfo.CurrentCulture) != "999wonders")
                {
                    System.Collections.Specialized.StringCollection Users = Properties.Settings.Default.UsersFlickr;
                    System.Collections.Specialized.StringCollection newUsers =
                            new System.Collections.Specialized.StringCollection();

                    if (Users != null && Users.Count > 0)
                    {
                        newUsers.Add(user_name);
                        foreach (string User_Name in Users)
                        {
                            //Application.Current.MessageBox(User_Name, User_Name);
                            if (!newUsers.Contains(User_Name))
                            {
                                newUsers.Add(User_Name);
                            }
                            if (newUsers.Count >= 3) break;
                        }
                    }
                    else
                    {
                        newUsers.Add(user_name);
                    }
                    Properties.Settings.Default.UsersFlickr = newUsers;
                    Properties.Settings.Default.Save();
                }

                FlickrUser flickrUser = new FlickrUser(user_name);

                User newUser = new User(flickrUser);
                GalleryPageFlickr page = newUser.CreateAlbumGalleryPageFlickr(item);
                AddExperience(newUser);
                Application.Current.GoToGallery(page);
            }
            else
            {
                Application.Current.MessageBox("Sorry, user not found", "User not found");
            }
            
            this.AppStatusText = "";
        }


        public void LogOn0(User user, MenuCategory item, string userName, bool saveUser)
        {
            if ((userName = userName.Trim().ToLower(CultureInfo.CurrentCulture)).Length == 0) return;

            this.AppStatusText = "Loading user [" + userName + "]...";

            //Simple way to get the time-consuming routine to run in the next round of message loop
            //to enable the UI to update the status text
            Timer t = new Timer();
            t.Data.Add("user", user);
            t.Data.Add("item", item);
            t.Data.Add("username", userName);
            t.Data.Add("saveuser", saveUser);
            t.Interval = 100;
            t.Tick += new EventHandler(LoadUser);
            t.Start();
        }

        /// <summary>
        /// Logon Flickr user
        /// </summary>
        /// <param name="user"></param>
        /// <param name="item"></param>
        /// <param name="userName"></param>
        /// <param name="saveUser"></param>
        public void LogOn1(User user, MenuCategory item, string userName, bool saveUser)
        {
            if ((userName = userName.Trim().ToLower(CultureInfo.CurrentCulture)).Length == 0) return;

            this.AppStatusText = "Loading user [" + userName + "]...";

            //Simple way to get the time-consuming routine to run in the next round of message loop
            //to enable the UI to update the status text
            Timer t = new Timer();
            t.Data.Add("user", user);
            t.Data.Add("item", item);
            t.Data.Add("username", userName);
            t.Data.Add("saveuser", saveUser);
            t.Interval = 100;
            t.Tick += new EventHandler(LoadUserFlickr);
            t.Start();
        }

        /// <summary>
        /// Navigate to the main menu.
        /// </summary>
        public void GoToMenu()
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Menu"] = menu;
            properties["Application"] = this;

            if (session != null)
            {
                session.GoToPage("resx://McePicasa/McePicasa.Resources/MainMenu", properties);
            }
            else
            {
                Log.LogIt("GoToMainMenu");
            }
        }

        /// <summary>
        /// Navigate to the search page.
        /// </summary>
        public void GoToSearch()
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Application"] = this;
            properties["Page"] = CreateSearchPage();

            if (session != null)
            {
                session.GoToPage("resx://McePicasa/McePicasa.Resources/SearchPage", properties);
            }
            else
            {
                Log.LogIt("GoToSearch");
            }
        }

        public void GoToSearch(SearchPage page)
        {
            if (page == null)
                throw new ArgumentNullException("page", "Must specify the model for the details page");

            // If we have no page session, just spit out a trace statement.
            if (session == null)
            {
                Log.LogIt("GoToSearch: " + page.Description + "\\" + page.SearchValue);
                return;
            }

            //
            // Construct the arguments dictionary and then navigate to the
            // details page template.
            //

            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Page"] = page;
            properties["Application"] = this;

            session.GoToPage("resx://McePicasa/McePicasa.Resources/SearchPage", properties);
        }


#if false
        /// <summary>
        /// Navigate to the downloads page.
        /// </summary>
        public void GoToDownloads()
        {
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Application"] = this;
            properties["Page"] = CreateDownloadsPage();

            if (session != null)
            {
                session.GoToPage("resx://McePicasa/McePicasa.Resources/DownloadsPage", properties);
            }
            else
            {
                Log.LogIt("GoToDownloads");
            }
        }
#endif

        /// <summary>
        /// Navigate to the specified details page.
        /// </summary>
        /// <param name="page">The state object for the details page</param>
        public void GoToDetails(DetailsPage page)
        {
            if (page == null)
                throw new ArgumentNullException("page", "Must specify the model for the details page");

            // If we have no page session, just spit out a trace statement.
            if (session == null)
            {
                Log.LogIt("GoToDetails: " + page.Title);
                return;
            }

            //
            // Construct the arguments dictionary and then navigate to the
            // details page template.
            //

            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Page"] = page;
            properties["Application"] = this;

            session.GoToPage("resx://McePicasa/McePicasa.Resources/DetailsPage", properties);
        }


        /// <summary>
        /// Navigate to the specified details page for flickr.
        /// </summary>
        /// <param name="page">The state object for the details page</param>
        public void GoToDetails(DetailsPageFlickr page)
        {
            if (page == null)
                throw new ArgumentNullException("page", "Must specify the model for the details page");

            // If we have no page session, just spit out a trace statement.
            if (session == null)
            {
                Log.LogIt("GoToDetails: " + page.Title);
                return;
            }

            //
            // Construct the arguments dictionary and then navigate to the
            // details page template.
            //

            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Page"] = page;
            properties["Application"] = this;

            session.GoToPage("resx://McePicasa/McePicasa.Resources/DetailsPageFlickrSearch", properties);
        }

        /// <summary>
        /// Navigate to the specified gallery page.
        /// </summary>
        /// <param name="page">The state object for the gallery page</param>
        public void GoToGallery(GalleryPage page)
        {
            if (page == null)
                throw new ArgumentNullException("page", "Must specify the model for the gallery page");

            // If we have no page session, just spit out a trace statement.
            if (session == null)
            {
                Log.LogIt("GoToGallery: " + page.Description);
                return;
            }

            //
            // Construct the arguments dictionary and then navigate to the
            // gallery page template.
            //

            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Page"] = page;
            properties["Application"] = this;

            session.GoToPage("resx://McePicasa/McePicasa.Resources/GalleryPage", properties);
        }

        /// <summary>
        /// Navigate to the specified gallery page. Flickr
        /// </summary>
        /// <param name="page">The state object for the gallery page</param>
        public void GoToGallery(GalleryPageFlickr page)
        {
            if (page == null)
                throw new ArgumentNullException("page", "Must specify the model for the gallery page");

            // If we have no page session, just spit out a trace statement.
            if (session == null)
            {
                Log.LogIt("GoToGallery: " + page.Description);
                return;
            }

            //
            // Construct the arguments dictionary and then navigate to the
            // gallery page template.
            //

            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Page"] = page;
            properties["Application"] = this;

            session.GoToPage("resx://McePicasa/McePicasa.Resources/GalleryPageFlickr", properties);
        }


        /// <summary>
        /// Navigate to the keyboard entry page.
        /// </summary>
        public void GoToKeyboard(EditableText text, bool password, bool submitWhenDone)
        {
            // If we have no page session, just spit out a trace statement.
            if (session == null)
            {
                Log.LogIt("GoToKeyboard: " + text.Value);
                return;
            }

            //
            // Construct the arguments dictionary and then navigate to the
            // keyboard page template.
            //

            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Model"] = text;
            properties["Password"] = password;
            properties["SubmitWhenDone"] = submitWhenDone;
            properties["Application"] = this;

            session.GoToPage("resx://McePicasa/McePicasa.Resources/KeyboardPage", properties);
        }

        /*
        public void GoToKeyboard(User user, MenuCategory category, bool submitWhenDone)
        {
            EditableText dummy = new EditableText();
            // If we have no page session, just spit out a trace statement.
            if (session == null)
            {
                Log.LogIt("GoToKeyboard!");
                return;
            }

            //
            // Construct the arguments dictionary and then navigate to the
            // keyboard page template.
            //

            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Model"] = dummy;
            properties["Password"] = false;
            properties["SubmitWhenDone"] = submitWhenDone;
            properties["Application"] = this;

            session.GoToPage("resx://McePicasa/McePicasa.Resources/KeyboardPage", properties);
        }
         * */

        /// <summary>
        /// Navigate back to the previous page.
        /// </summary>
        public void GoBack()
        {
            if (session == null)
            {
                Log.LogIt("GoBack");
                return;
            }
            session.BackPage();
        }

        /// <summary>
        /// Construct a SearchPage object. This is carried out by the Application
        /// because the search page is global and not owned by any single 
        /// Experience.
        /// NOTE: This is public to enable debug markup access.
        /// </summary>
        public SearchPage CreateSearchPage()
        {
            SearchPage page = new SearchPage();
            page.Description = McePicasa.Resources.Search;
            page.SetContent(new VirtualList());

            //
            // Create one on/off filter for each experience.
            //

            ArrayListDataSet filters = new ArrayListDataSet(page);
            foreach (ApplicationExperience experience in experiences)
            {
                filters.Add(CreateSearchPageFilter(page, experience));
            }
            page.SetFilters(filters);

            return page;
        }

        /// <summary>
        /// Create a search filter object given an Experience.
        /// </summary>
        private static BooleanChoice CreateSearchPageFilter(SearchPage page, ApplicationExperience experience)
        {
            BooleanChoice filter = new BooleanChoice(page, experience.Description);

            // Stick the associated experience in dynamic data so we can fetch it later.
            filter.Data["Experience"] = experience;

            // Default the filter to "on".
            filter.Value = true;

            return filter;
        }

#if false
        /// <summary>
        /// Construct a DownloadsPage object. This is carried out by the Application
        /// because the downloads page is global and not owned by any single 
        /// Experience.
        /// NOTE: This is public to enable debug markup access.
        /// </summary>
        public DownloadsPage CreateDownloadsPage()
        {
            DownloadsPage page = new DownloadsPage();
            page.Description = McePicasa.Resources.Downloads;

            //
            // Create one filter for each experience and one global "All"
            // filter that indicates all experiences.
            //

            /*
            ArrayListDataSet filters = new ArrayListDataSet(page);
            //filters.Add(new DownloadFilter(page, "All", ActiveDownloads));
            foreach (ApplicationExperience experience in experiences)
            {
                filters.Add(CreateDownloadPageFilter(page, experience));
            }
            Choice filtersChoice = new Choice(page, null, filters);

            // Default to having "All" as the current filter
            filtersChoice.ChosenIndex = 0;

            page.Filters = filtersChoice;
            */

            return page;
        }
#endif

        /// <summary>
        /// Displays a simple "OK" message box, branded with the McePicasa logo.
        /// </summary>
        public void MessageBox(string text)
        {
            MessageBox(text, "McePicasa", 5, true);
        }

        /// <summary>
        /// Displays a simple "OK" message box, branded with the McePicasa logo.
        /// </summary>
        public void MessageBox(string text, string caption)
        {
            MessageBox(text, caption, 5, true);
        }

        /// <summary>
        /// Displays a simple "OK" message box, branded with the McePicasa logo.
        /// </summary>
        public void MessageBox(string text, string caption, int timeout, bool modal)
        {
            if (MediaCenterEnvironment == null)
            {
                Log.LogIt(String.Format(CultureInfo.CurrentCulture, "Dialog {0}: {1}", caption, text));
                return;
            }

            MediaCenterEnvironment.Dialog(
                text,
                caption,
                new object[] { DialogButtons.Ok },
                timeout,
                modal,
                Global.IconImagePath,
                delegate(DialogResult dialogResult) { });
        }

        public User CurrentUser
        {
            get { return currentUser; }
            set { currentUser = value; }
        }

        public static void GoUp()
        {
            Application.Current.MediaCenterEnvironment.NavigateToPage(PageId.MorePrograms, null);
        }

        public string MyVersion
        {
            get { return myVersion; }
            set { myVersion = value; }
        }

        public string NewVersion
        {
            get { return newVersion; }
            set { newVersion = value; }
        }

        public string AppDataPath()
        {
            return appDataPath;
        }

        public string AppStatusText
        {
            get { return appStatusText; }
            set
            {
                if (appStatusText != value)
                {
                    appStatusText = value;
                    FirePropertyChanged("AppStatusText");
                }
            }
        }

        public SearchPage CreateSettingPage()
        {
            SearchPage page = new SearchPage();
            page.Description = McePicasa.Resources.Setting;
            page.SetContent(new VirtualList());

            //
            // Create one on/off filter for each experience.
            //

            /*
            ArrayListDataSet filters = new ArrayListDataSet(page);
            foreach (ApplicationExperience experience in experiences)
            {
                filters.Add(CreateSearchPageFilter(page, experience));
            }
            page.SetFilters(filters);
             * */

            return page;
        }

        public void GoToSettingPage()
        {
            Log.LogIt("GoToSettingPage: Oops!!! Sorry, setting page has not been implemented!");

            /*
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties["Application"] = this;
            properties["Page"] = CreateSettingPage();

            if (session != null)
            {
                session.GoToPage("resx://McePicasa/McePicasa.Resources/Settings", properties);
            }
            else
            {
                Log.LogIt("GoToSettingPage");
            }
             * */            
        }

        public static void DeleteCachePhotos()
        {
            DialogResult r = Application.Current.MediaCenterEnvironment.Dialog(
                    "Are you sure you want to delete all local cache photos?",
                    "McePicasa - " + Application.Current.AppDataPath(),
                    DialogButtons.Yes | DialogButtons.No, 20, true);

            if (r == DialogResult.Yes)
            {
                try
                {
                    Directory.Delete(Application.Current.AppDataPath(), true);
                }
                catch (IOException e)
                {
                    Global.MsgBox("IOException: " + e.Message, 20);
                }
                catch (UnauthorizedAccessException e)
                {
                    Global.MsgBox("UnauthorizedAccessException: " + e.Message, 20);
                }
                catch
                {
                    throw;
                }
            }
        }

        private static void TrackUsers()
        {
            System.Threading.Thread myThread = new System.Threading.Thread
              (delegate()
              {
                  PicasaAPI.TrackUser();
              });
            myThread.Start();
        }

        private string appStatusText = "";
        private static Application singleApplicationInstance;
        private HistoryOrientedPageSession session;
        private AddInHost host;
        private ArrayListDataSet menu = new ArrayListDataSet();
        private List<ApplicationExperience> experiences = new List<ApplicationExperience>();
        private string username;
        private User currentUser;
        private string myVersion;
        private string newVersion;
        private string appDataPath;
        //private int totalUsers;
        //private int userCount;
    }
}
